package cn.itcast.controller;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

@RestController
public class XywcController {
    String blindUrl="http://app.xywc88.cn/api/rush_buy/blind";
    String createOrderUrl="http://app.xywc88.cn/api/blind_order/createOrders";
    Integer goodsId=1;
    String ipUrl="http://ip.donepx.com";
    private static List<IpBean> list=new ArrayList<>();
    private static List<String> tokenList=new ArrayList<>();
    @RequestMapping("/hello3")
    public String hello3(@RequestParam(value = "num",defaultValue = "1000") Integer num) {
        if(CollectionUtils.isEmpty(list)){
            try{
                String result = HttpRequest.get(ipUrl).execute().body();
                JSONArray json = JSONUtil.parseArray(result);
                for(int i=0;i<json.size();i++) {
                    JSONObject jsonObject=json.getJSONObject(i);
                    IpBean ipBean = new IpBean();
                    ipBean.setIpAddress(jsonObject.get("ip").toString());
                    ipBean.setPort(Integer.parseInt(jsonObject.get("port").toString()));
                    list.add(ipBean);
                }
            }catch (Exception e){
                IpBean ipBean=new IpBean();
                list.add(ipBean);
            }
        }
        if(CollectionUtils.isEmpty(tokenList)){
            addToken();
        }
        for(int i=0;i<num;i++){
            try {
                for(int j=0;j<list.size();j++){
                    for(int z=0;z<tokenList.size();z++){
                        IpBean ipBean=list.get(j);
                        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipBean.getIpAddress(), ipBean.getPort()));
                        int finalZ = z;
                        proxy=null;
                        ThreadUtil.execAsync(() -> {
                            confireOrder(null,tokenList.get(finalZ));
                        });
                    }

                }
            } catch (Exception e) {
                System.out.println("#####异常####" + e.getMessage());
            }
        }
        return null;
    }
    public void confireOrder(Proxy proxy,String token){
        JSONObject requestJson = new JSONObject();
        requestJson.putOnce("goods_id", goodsId);
        requestJson.putOnce("token", token);
        String result =null;
        try{
            String requestParam = requestJson.toString();
            result = HttpRequest.post(blindUrl).body(requestParam).timeout(3000)
                    .setProxy(proxy).execute().body();
            System.out.println("#####创建订单返回参数####"+result+"#######"+System.currentTimeMillis()+"#####"+token);
            if(StrUtil.isNotBlank(result)){
                createOrder(proxy,token);
            }

        }catch (Exception e){
            System.out.println("#####创建订单失败####"+e.getMessage()+"###"+token);
        }
    }
    public void createOrder(Proxy proxy,String token){
        JSONObject requestJson = new JSONObject();
        requestJson.putOnce("goods_id", goodsId);
        requestJson.putOnce("token", token);
        requestJson.putOnce("pay_type", 3);
        String requestParam = requestJson.toString();
        String result = HttpRequest.post(createOrderUrl).setProxy(proxy).timeout(3000).body(requestParam).execute().body();
        System.out.println("####支付结果######"+result+"########"+token);
    }
    public void addToken(){
        tokenList.add("Ze7FXsCYAI=G9450a+jWbnnakLdNyHrZr1MYviutqjqqdCsHau=I+m0KQpxXhIZFHj6KrKitBIjEdFoDnejYb4k3qihK8dZVxndXH4mZBzuno7xX8VaIAGc-RKZV4-L9-xnET");
        tokenList.add("=4dS1UrqwP8TgPfmCrwolUtl_WsX4VZRtQU0_KqVll3Ck9DTDb_9kbeV5_L6OWvyJ9cXwaaqmF-7kPx9PeA8mftYESa7QeCOJQITAKfeytFHlSDVzan9Lwi06Vb4_bh11lvR2");
    }
}
